<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>打开一个终端</title>
</head>
<body>
<script>
    function fd() {
        var url = window.document.location.href.toString(); //当前完整url
        var u = url.split("?"); //以？为分隔符把url转换成字符串数组
        if (typeof(u[1]) == "string") {
            u = u[1].split("&"); //同上
            var get = {};
            for (var i in u) {
                var j = u[i].split("="); //同上
                if(j[0] == 'fd') return j[1];
            }
        }
        throw 'url上必须携带fd参数';
    }
    function getcookie(name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) {
            return unescape(arr[2]);
        } else {
            return null;
        }
    }
    var socket = new WebSocket('ws://'+window.location.host+'/v1/terminal.openSsh/'+fd()+"?_token="+getcookie("token"));
    socket.onmessage = function (evt) {
        console.log(evt.data);
        var data = JSON.parse(evt.data);
        var fd = data.body;
        if(fd && isNaN(fd) == false){
            window.location = 'terminal.html?fd='+fd;
        }else{
            document.body.innerHTML = "无法连接到终端";
        }
    }
    socket.onclose = function (evt) {
    }
    socket.onerror = function (evt) {
    }
</script>
</body>
</html>